/* eslint-disable no-console */
import { createRouter, createWebHistory } from 'vue-router';
import routes from './routes';
import { ElMessage } from 'element-plus';
// import { store } from '@/store/index'

const scrollBehavior = () => {
    return { left: 0, top: 0 };
};

const router = createRouter({
    history: createWebHistory(),
    routes,
    scrollBehavior
});

router.onError(async (err, to, from) => {
    console.log(
        '路由错误 :    ',
        '\n-- to --  \n',
        to,
        '\n-- from --  \n',
        from,
        '\n-- err --  \n',
        JSON.parse(JSON.stringify(err))
    );
    const jsModulePattern = /dynamically imported module/g;
    const jsLoadPattern = /Failed to load module script/g;
    const isFailed = err.message.match(jsModulePattern || jsLoadPattern);
    // if (isFailed) {
    //   // location.reload()
    //   location.href = to.fullPath
    // }
    try {
        location.replace(to.fullPath);
    } catch (err) {
        console.log('err err router router  - - -   ', err);
        location.href = to.fullPath;
    }
});

export default router;
